(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.apProgram.updateContinueController', updateContinueController);
	updateContinueController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService', 'apProgramService', '$filter' ];
	function updateContinueController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, apProgramService, $filter) {
		var ctrl = this, apProgramInfo = $scope.$stateParams.params, errorMessage = {};
		//ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.initValidatorConfig = angular.copy(ctrl.validatorConfig);
		ctrl.validatorConfig = {};
		ctrl.apProgram = {};
		
		ctrl.init = function() {
			var params = {
					taskId : apProgramInfo.taskId
			};
			// init acct
			apProgramService.initProfAcct(layoutConfig, $scope, ctrl, true);
			// init int
			//apProgramService.initProfInt(layoutConfig, $scope, ctrl, true);
			//apProgramService.initProfCostOfFundInt(layoutConfig, $scope, ctrl, true);
			// init pastdue
			apProgramService.initProfPastdue(layoutConfig, $scope, ctrl, true);
			// init fee
			apProgramService.initProfFee(layoutConfig, $scope, ctrl, true);
			// init doc
			apProgramService.initEventDoccfg(layoutConfig, $scope, ctrl, true);
			// init limit facility
			//apProgramService.initProfLimitFaci(layoutConfig, $scope, ctrl, true);
			//apProgramService.initProfLimitInsFaci(layoutConfig, $scope, ctrl, true);
			//apProgramService.initProfLimitPurFaci(layoutConfig, $scope, ctrl, true);
			// init Settlement Instruction
			apProgramService.initProfSetlinst(layoutConfig, $scope, ctrl, true);
			// init cust prop define
			apProgramService.initCustProp(layoutConfig, $scope, ctrl, true);
			//
			apProgramService.initGeneral($scope, ctrl, true).then(function() {
				authService.updateContinue(params).then(function(response) {
					var initApProgram = response.data.model;
					ctrl.action = "updateContinue";
					apProgramService.initLayoutConfigAndModel($scope, ctrl, initApProgram, ctrl.action, true).then(function() {
						// ctrl.apProgram = initApProgram; done in initLayoutConfig
						ctrl.apProgram.taskId = response.data.task.taskId || apProgramInfo.taskId;
					});
				});
			});
		};

		ctrl.updateContinueSave = function() {
			//
			apProgramService.clearDirtyData(ctrl);
			return authService.updateContinueSave(ctrl.apProgram).then(saveSuccessful, saveFailed);
		};

		ctrl.updateContinueSubmit = function() {
			//
			apProgramService.clearDirtyData(ctrl);
			return authService.updateContinueSubmit(ctrl.apProgram).then(submitSuccessful, submitFailed);
		};
		
		ctrl.updateContinueCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [$filter("encode")(ctrl.apProgram.buyerCode)]} ],
				'btnCallback' : {
					'ok' : function(response) {
						authService.updateContinueCancel(ctrl.apProgram).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};
		
		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.approgram.list',
					'state' : 'cda.apProgram.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				angular.merge(ctrl.apProgram, response.data.model);
			} else if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.approgram.list',
					'state' : 'cda.apProgram.list',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);
